package com.cwj.film.controller;

import com.cwj.common.domain.film;
import com.cwj.common.vo.Result;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Controller
@RequestMapping("/upFilm")
public class upFilmController {

    @Resource
   private com.cwj.film.service.inter.upFilm upFilm;
    @Resource
    private HttpServletRequest request;
    @Resource
    private ServletContext servletContext;
    @PostMapping("/filmExist")
    @ResponseBody
    private Result filmExist(film film) throws JsonProcessingException {

        boolean exist = upFilm.filmExist(film);
        return Result.normal(exist,null);
    }
    @PostMapping("/upFilm")
    private String upFilm(film film, @RequestParam(name = "pict") MultipartFile pictFile, @RequestParam(name = "film") MultipartFile[] filmFiles) throws IOException {

        request.setAttribute("film",film);
        boolean up = upFilm.upFilm(film, pictFile,filmFiles);
        Object filmUrl = servletContext.getAttribute("FilmUrl");
        return up ? "redirect:"+filmUrl:"redirect:"+ filmUrl.toString();
    }
}
